package com.cyw.common.domain.common;

import com.cyw.common.utils.BaseUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页返回对象
 *
 * @Author:cyw
 * @CreateTime: 2021/1/22 18:12
 **/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class PageWrapper<T> implements Serializable {

    private static final long serialVersionUID = 5615492629280762747L;

    /**
     * 当前页
     */
    private int pageNum;

    /**
     * 每页的数量
     */
    private int pageSize;

    /**
     * 总记录数
     */
    private long total;

    /**
     * 总页数
     */
    private int pages;

    /**
     * 结果集
     */
    private List<T> list;

    public PageWrapper() {
    }

    public PageWrapper(int pageNum, int pageSize, long total, int pages, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.pages = pages;
        this.list = list;
    }

    public static <T> PageWrapper<T> empty(PageWrapper<?> pageWrapper) {
        return new PageWrapper<T>(pageWrapper.getPageNum(), pageWrapper.getPageSize(), pageWrapper.getTotal(), pageWrapper.getPages(), Lists.newArrayList());
    }

    public static <T> PageWrapper<T> empty() {
        return new PageWrapper<T>(0, 0, 0, 0, Lists.newArrayList());
    }

    public Boolean isEmpty() {
        return BaseUtils.isEmpty(this.list);
    }

    /**
     * 转换page，并设置list
     *
     * @param page
     * @param list
     * @return
     */
    public static <V> PageWrapper<V> convert(Page<?> page, List<V> list) {
        return new PageWrapper<>(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getPages(), listIfPresent(list));
    }

    /**
     * 转换page，并设置list
     *
     * @param pageWrapper
     * @param list
     * @return
     */
    public static <V> PageWrapper<V> convert(PageWrapper<?> pageWrapper, List<V> list) {
        return new PageWrapper<>(pageWrapper.getPageNum(), pageWrapper.getPageSize(), pageWrapper.getTotal(), pageWrapper.getPages(), listIfPresent(list));
    }

    public static <V> PageWrapper<V> convert(Page<V> page) {
        return new PageWrapper<V>(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getPages(), page.getResult());
    }

    public static <V> PageWrapper<V> convert(PageInfo<V> pageInfo) {
        return new PageWrapper<V>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    public static <V> PageWrapper<V> convert(PageInfo<?> pageInfo, List<V> list) {
        return new PageWrapper<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), listIfPresent(list));
    }

    private static <V> List<V> listIfPresent(List<V> list) {
        return list == null ? new ArrayList<>() : list;
    }

}

    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    